package cn.gwm.flink.streaming.beans;


import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * Author : GW00280745
 * Date : 2022/5/18 10:16
 * Description :{"id":"","type":"","body":{}}
 */
public class GwmKafkaJson {
    private String id;
    private String type;
    private Long timestamp;
    //        private String version;
    //        private Long timestamp;
    //        private String source;
    private Map<String, Object> body;

    private GwmKafkaJson() {
    }

    private GwmKafkaJson(Builder builder) {
        this.id = builder.id;
        this.type = builder.type;
        this.timestamp = builder.timestamp;
        this.body = builder.body;
    }

    public static class Builder {
        private String id;
        private String type;
        private Long timestamp;
        private Map<String, Object> body;

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder type(String type) {
            this.type = type;
            return this;
        }

        public Builder timestamp(Long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public Builder body(Map<String, Object> body) {
            this.body = body;
            return this;
        }

        public Builder sourceBody(Map<String, Object> body) {
            JSONObject source = new JSONObject();
            source.put("source", body);
            this.id = (String) body.get("deviceId");
            this.body = source;
            return this;
        }

        public JSONObject build() {
            GwmKafkaJson gwmKafkaJson = new GwmKafkaJson(this);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", gwmKafkaJson.id);
            jsonObject.put("type", gwmKafkaJson.type);
            jsonObject.put("timestamp", gwmKafkaJson.timestamp);
            JSONObject body = new JSONObject();
            body.putAll(gwmKafkaJson.body);
            jsonObject.put("body", body);
            return jsonObject;
        }
    }

    public static Builder builder() {
        return new Builder();
    }
}
